package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.entity.ResultModel;
import com.example.demo.entity.Student;
import com.example.demo.mapper.StudentMapper;
import com.example.demo.service.StudentService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@AllArgsConstructor
@RequestMapping("student")
public class StudentController {

//    public final InmoneyMapper inmoneyMapper; //充值的日志账单接口
    public final StudentMapper studentMapper; //引入学生接口
    public final StudentService service;  //引入学生接口
    @PutMapping("insert") //增加学生
    public ResultModel add(Student student){
        Integer count = studentMapper.saveStudent(student);
        return ResultModel.ok(String.format("插入成功，返回受影响行数 %d",count));
    }
    @GetMapping("listStudent")//Get的请求方式,获取全部学生
    public ResultModel list() {
        return ResultModel.ok(studentMapper.selectStudents());
    }
    @GetMapping("get/{stuId}")      //根据学号获取一个学生
    public ResultModel get(@PathVariable Integer stuId) {
        return ResultModel.ok(studentMapper.getStudentByStuId(stuId));
    }
    @DeleteMapping("delete/{stuId}")   //对应删除，表明是一个删除URL映射”
    public ResultModel delete(@PathVariable Integer stuId) {
        return ResultModel.ok(String.format("删除成功，受影响行数为 %d", studentMapper.deleteStudentById(stuId)));
    }
    @PostMapping("update")      //Post的请求方式,更新学生
    public void update(Student student) {
        studentMapper.updateStudent(student);
        // 取出最新一条账单
//        Inmoneylog newInmoney = inmoneyMapper.selectNewInmoney(student.getStuId());//这个学生的最新充值记录
//        System.out.println(newInmoney.getInMoney()+"sssssssss");
//        //更新的时候（存钱） 生成账单
//        Date utilDate = new Date();//util.Date.
//        java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
//        Inmoneylog money = new Inmoneylog(student.getStuId(),student.getStuAge(),sqlDate);
//        inmoneyMapper.saveInMoneyLog(money);
//        return ResultModel.ok("修改成功");
    }
    @GetMapping("getByPageStu")
    public ResultModel getByPageStu(Page page, String stuName){
        IPage list = service.page(page,new QueryWrapper<Student>().like(stuName != null,"stuName",stuName));
        return ResultModel.ok(list.getRecords(),list.getTotal());
    }
}
